package com.prd.config;

import feign.RequestInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author lizhaoyi
 * @date 2023/10/23 16:15
 * Description:
 */

@Configuration
@Slf4j
public class InterceptorConfig implements WebMvcConfigurer {


    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        registry.addInterceptor(new LoginInterceptor())
                //拦截的路径
                .addPathPatterns("/api/attendance/*/**"
                        ,"/api/ot/*/**"
                        ,"/api/outSide/*/**"
                        ,"/api/attendanceVocation/*/**"
                        ,"/api/workOrder/*/**"
                        ,"/api/workContactForm/*/**"
                        ,"/api/workReport/*/**"
                        ,"/api/oaDepart/*/**"
                        ,"/api/oaDepartEvaluate/*/**"
                        ,"/api/oaDepartWorkHandover/*/**"
                        ,"/api/oaOfferLetter/*/**")

                //排查不拦截的路径
                .excludePathPatterns("");

    }

    @Bean("requestInterceptor")
    public RequestInterceptor requestInterceptor(){
        return template -> {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            if(attributes!=null){
                HttpServletRequest request = attributes.getRequest();
                if (null == request){
                    return;
                }

                log.info(request.getHeaderNames().toString());
                template.header("token", request.getHeader("token"));
            }else {
                log.warn("requestInterceptor获取 Header空指针异常");
            }
        };
    }
}
